package com.ant.backstage.service.impl;

import com.ant.backstage.facade.ApiReturn;
import com.ant.backstage.facade.code.ApiReturnCode;
import com.ant.backstage.service.EnumService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("enumService")
public class EnumServiceImpl implements EnumService {

    @Override
    public ApiReturn getEnumList(String name) {
        ApiReturn apiReturn = new ApiReturn();
        try {
            String[] names = name.split(",");
            Map<String, List<Object>> maps = new HashMap<>();
            for (String nm : names) {
                String enumPackage = "com.ant.backstage.facade.enums";
                Class<Enum> clazz = (Class<Enum>) Class.forName(enumPackage + "." + nm);
                Enum[] objs = clazz.getEnumConstants();
                List<Object> ls = new ArrayList<>();
                for (Enum obj : objs) {
                    ls.add(obj);
                }
                maps.put(nm, ls);
            }
            apiReturn.setApiReturnCode(ApiReturnCode.SUCCESSFUL);
            apiReturn.setData(maps);
        } catch (Exception e) {
            apiReturn.setCode(ApiReturnCode.PARAMS_ERROR.getCode());
            apiReturn.setMsg(ApiReturnCode.PARAMS_ERROR.getMessage());
            return apiReturn;
        }
        return apiReturn;
    }
}
